<?php if(!defined('OCTOMS')){header('HTTP/1.1 403');die('{"error":"forbidden"}');}
/*
 * @package       OctoMS.com
 * @subpackage    DesignJotter
 * @link          http://DesignJotter.com
 * @copyright     Copyright 2012, Valentino-Jivko Radosavlevici (http://valentino.radosavlevici.com)
 * @license       GPL v3.0 (http://www.gnu.org/licenses/gpl-3.0.txt)
 * 
 * Redistributions of files must retain the above copyright notice.
 * 
 * @since         OctoMS 0.0.1
 */
	
	/*
	 * The main controller
	 * 
	 * @link http://octoms.com/doc/main
	 * 
	 * @package OctoMS
	 * @subpackage WP-OctoMS
	 * @version 0.1
	 * 
	 * @author Valentino-Jivko Radosavlevici
	 */
	class _main extends octoms
	{
		
		/**
		 * Class constructor
		 * 
		 * @return main
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function __construct()
		{
			// Load the model
			$this->_('main',OMS_M,'model');
			
		}// end function __construct()
		
		/**
		 * Hello!
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function welcome()
		{
			// Set the title
			$this->template->title('Welcome to your "'.WP_OCTOMS_TITLE.'" DesignJotter theme!');

			// Load the CSS and JS
			$this->template
				->scriptroot(true)
				->css('tips')
				->js('tips');
				
			// Load the helper
			$this->template->help(array(
				'theme'=>WP_OCTOMS_TITLE,
				'version'=>WP_OCTOMS_VERSION,
				'acronym'=>WP_OCTOMS_VERSION_ACRONYM,
				'form'=>$this->template->widget(
					'facebook',
					'comments',
					array(
						10,
						null,
						'light',
						'http://theme.octoms.com/'.strtolower(WP_OCTOMS_TITLE),
						'326567857423968'
					),
					null,
					true
				)
			));
			
			// Get the last completed step
			if (is_null( $step = $this->settings->get('install_last_step')))
			{
				call_user_func(array($this,'_step1'));
			}
			else 
			{
				call_user_func(array($this,'_step'.$step));
			}
			
		}// end function welcome()
		
		/**
		 * Advance helper
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _advance($method=null,$step=NULL)
		{
			// Get the current step
			if (!is_null($method))
			{
				$current_step = preg_replace('/[^0-9]*/i', '', $method);
			}
			else 
			{
				return false;
			}
			
			// Get the next step
			if (!is_null($step))
			{
				$step = intval($step);
			}
			else 
			{
				$step = $current_step+1;
			}
			
			// Verify user input
			if (!method_exists($this, '_step'.$step))
			{
				return false;
			}
			
			// Set the last step for the static call
			$this->settings->set('install_last_step',$step);
			
			// Start the buffer
			ob_start();
			
			// No post for this call
			$_POST = array();
			
			// Get the data
			call_user_func(array($this,'_step'.$step));
			
			// Reactivate the AJAX capabilities
			$_POST = array('#*#'=>'true');
			
			// Save it for the AJAX call
			$this->template
				->_api(array('wp_replace'))
				->_api_cnt(ob_get_clean());
			
		}// end function _advance()
		
		/**
		 * Step 1.
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _step1()
		{
			if ($this->input->post())
			{
				// All done
				$this->template->_api('Success!');
				
				// Advance
				$this->_advance(__METHOD__);
			}
						
			// Display the form
			$this->view->display('install/step1');
			
		}// end function _step1()
		
		/**
		 * Step 2.
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _step2()
		{			
			if ($this->input->post())
			{
				// Perform the changes
				$advance = $this->model->step2_save();

				// Advance
				$this->_advance(__METHOD__,$advance?null:2);
			}

			// Display the form
			$this->view->display(
				'install/step2',
				array(
					'data' => $this->model->step2_check()
				)
			);
			
		}// end function _step2()
		
		/**
		 * Step 3.
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _step3()
		{
			if ($this->input->post())
			{
				if ($this->input->post('dummy') == 'true')
				{
					// Perform the changes
					$advance = $this->model->step3_save();
					
					// Advance
					$this->_advance(__METHOD__,$advance?null:3);
				}
				else 
				{
					// All done
					$this->template->_api('Success!');
					
					// Advance
					$this->_advance(__METHOD__);
				}
			}
			
			// Display the form
			$this->view->display(
				'install/step3',
				array(
					'data' => $this->model->step3_check()
				)
			);
			
		}// end function _step3()
		
		/**
		 * Step 4.
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _step4()
		{			
			// Load the tips
			if ($this->input->post('get_tips'))
			{
				// Display the data
				$this->template
					->_api_cnt(
						$this->view->load(
							'install/tips',
							$this->model->tips()
						)
					)
					->_api(
						array(
							'wp_replace',
							'tips'
						)
					);
			}
			
			// Display the data
			$this->view->display(
				'install/tips',
				array(
					'data' => '<span class="loading">Loading tips...</span>',
					'first' => true
				)
			);
			
		}// end function _step4()
		
		/**
		 * Website identity
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function identity()
		{
			// Set the title
			$this->template->title('Website identity');
			
			// Check wether the GD extension is loaded
			if(!in_array('gd', get_loaded_extensions()))
			{
				$ext = '<b style="color:red;">NOT loaded</b>';
			}
			else 
			{
				$ext = '<b style="color:green;">loaded</b>';
			}
			$this->template->help(array('ext'=>$ext,'logo'=>ROOT.DS.'upload'.DS.'front'.DS.'logo.png'));
			
			// Handle the upload
			if ($this->input->file('logo'))
			{
				$this->model->logo_upload('logo');
			}
			
			// Get the upload form
			$this->view->display(
				'logo',
				array(
					'headline'=>$this->settings->get('headline'),
					'copyright'=>$this->settings->get('copyright'),
					'logo_text'=>$this->settings->get('logo_text'),
					'welcome'=>$this->settings->get('welcome'),
					'front_text'=>$this->settings->get('front_text')
				)
			);
		}// end function identity()
		
		/**
		 * Front page
		 * 
		 * @example 
		 * @link 
		 * 
		 * @param 
		 * @return 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function front_page()
		{
			// Set the title
			$this->template->title('Front page settings');
			
			// Set the restrictions
			$restr = array(
				'w'=>1800,
				'h'=>1000
			);
			
			// Handle the upload
			if ($this->input->file('header'))
			{
				$this->model->index_upload('header',$restr['w'],$restr['h']);
			}
			// Or just update the fields
			elseif ($this->input->post())
			{
				$this->model->index_update();
			}
			
			// Load the CSS
			$this->template
				->scriptroot(true)
				->css('front_page')
				->js('front_page');
			
			// Load the template
			$this->view->display(
				'front_page',
				array(
					'restr' => $restr,
					'patterns' => 5,
					'pattern' => $this->settings->get('body_class')
				)
			);
			
		}// end function front_page()
		
		/**
		 * Custom categories
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function categories()
		{
			// Set the title
			$this->template->title('Custom category logos');
			
			// Easter eggs?
			if (count(func_get_args())>0)
			{
				$args = func_get_args();
				return call_user_func_array(array($this,'_categories_css'),$args);
			}
			
			// Update the categories?
			if ($this->input->post()) 
			{
				// Update the categories
				$this->model->categories_update();
			}
			
			// Load the javascript and CSS
			wp_enqueue_style(
				md5('categories.css'),
				get_template_directory_uri().'/categories.css',
				false,
				'1.0',
				'all'
			);
			$this->template
				->scriptroot(true)
				->css('categories')
				->js('categories');
			
			// Load the template
			$this->view->display(
				'categories',
				array(
					'data'=>$this->model->categories_get(),
					'icons'=>$this->model->categories_icons(60,60,'symbols.png')
				)
			);
			
		}// end function categories()
		
		/**
		 * Hidden functionality to create the CSS stylesheet
		 * 
		 * @param int $iconWidth
		 * @param int $iconHeight
		 * @param string $name
		 */
		function _categories_css($iconWidth=null,$iconHeight=null,$name=null,$pass=null)
		{
			// Don't allow whisperers
			if (is_null($iconWidth) || is_null($iconHeight) || is_null($name) || $pass != 'qwerty')
			{
				$this->template->_api('Silence is golden.','info');
			}
			else 
			{
				// Format the standard icon width and height
				$iconWidth  = intval($iconWidth);
				$iconHeight = intval($iconHeight);
				
				// Format the name
				$name = strval($name);
				
				// If the file exists
				if (file_exists($f = ROOT.DS.'img'.DS.$name)) 
				{
					// Load the image library
					$this->_('image');
				
					// Load the image to get its properties
					$img = $this->image->load($f,true);
					
					// Prepare the result
					$result = array();
					
					// Columns
					$cols = $img['w']/$iconWidth;
					
					// Lines
					$lines = $img['h']/$iconHeight;
					
					// Prepare the result
					$result = 
					'/*Generated by DesignJotter ['.date('Y-m-d,H:i:s').']*/'."\n".
					'.octoms-category {'."\n".
						"\t".'background: url(img/'.$name.') no-repeat 0 0 transparent;'."\n".
						"\t".'display: block;'."\n".
						"\t".'height: '.$iconHeight.'px;'."\n".
						"\t".'margin: 0 auto;'."\n".
						"\t".'width: '.$iconWidth.'px;'."\n".
					'}'."\n\n";
					
					// Create the lines
					for ($j=1; $j<=$lines; $j++)
					{
						// Y shift
						$y = $iconHeight*($j-1)*(-1);
						for ($i=1; $i<=$cols; $i++)
						{
							// X shift
							$x = $iconWidth*($i-1)*(-1);
							
							// Append the result
							$result .= '.octoms-category.c'.$i.'.l'.$j.' {'."\n".
								"\t".'background-position: '.$x.'px '.$y.'px;'."\n".
							'}'."\n\n";
						}
					}
					
					// Rewrite the file
					$fh = @fopen(ROOT.DS.'categories.css', 'w');
					fwrite($fh, $result);
					fclose($fh);
					
					// All done
					$this->template->_api(sprintf(
						'File "%s" was rewritten for "%s" with %s lines and %s columns.',
						ROOT.DS.'categories.css',
						$name,
						$lines,
						$cols
					));
				}
				else 
				{
					$this->template->_api('File "'.$f.'"not found.',false);
				}
			}
			
			// Reload :)
			$this->template->_r('_main/categories');
		}
		
		/**
		 * Custom colors
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function custom_colors()
		{
			// Set the title
			$this->template->title('Custom theme colors');
			
			// Set the helper data
			$this->template->help(array('file'=>ROOT.DS.'style-global.css'));
			
			// Update the fields?
			if ($this->input->post())
			{
				$this->model->css_update();
			}
			
			// Load the JS and CSS
			$this->template
				->scriptroot('content','colorpicker')
				->js('color_picker')
				->js('eye')
				->js('utils')
				->css('colorpicker');
			
			// Load the view
			$this->view->display(
				'custom_colors',
				array(
					'data' => $this->model->css_marks()
				)
			);
			
		}// end function custom_colors()
		
		/**
		 * Slider
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _slider()
		{
			// Set the title
			$this->template->title('Front page slider');
			
			// Check wether the GD extension is loaded
			if(!in_array('gd', get_loaded_extensions()))
			{
				$ext = '<b style="color:red;">NOT loaded</b>';
			}
			else 
			{
				$ext = '<b style="color:green;">loaded</b>';
			}
			$this->template->help(array('ext'=>$ext,'images'=>ROOT.DS.'upload'.DS.'front'.DS));
			
			
			// Uploading a file?
			if ($this->input->file('image'))
			{
				$this->model->front_add_image('image');
			}
			// Updating the information
			elseif ($this->input->post())
			{
				$this->model->front_update();
			}
			
			// Load the lightbox
			$this->template
				->scriptroot('content','lightbox')
				->js('lightbox')
				->css('lightbox');
			
			// Load the view
			print $this->view->load(
				'slider',
				array(
					'images'=>$this->model->front_list(),
					'effects'=>$this->model->front_effects()
				)
			);
		}// end function slider()
		
		/**
		 * Facebook config page
		 * 
		 * @example 
		 * 
		 * @param 
		 * @return 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function facebook()
		{
			// Set the title
			$this->template->title('Facebook configuration page');
			
			// Update the data
			if($this->input->post())
			{
				$this->model->facebook_update();
			}
			
			// Display the form
			$this->view->display(
				'facebook',
				$this->model->facebook_get()
			);
			
		}// end function facebook()
		
		/**
		 * Identity page
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function social()
		{
			// Set the title
			$this->template->title('Social accounts');
			
			// Update the data?
			if ($this->input->post())
			{
				if (null !== $this->input->post('_reset_'))
				{
					$this->model->identity_reset();
				}
				else 
				{
					$this->model->identity_update();
				}
			}
			
			// Load the identity template
			$this->view->display(
				'identity',
				array(
					'social' => $this->model->identity_accounts(),
					'data' => $this->model->identity_read()
				)
			);
			
		}// end function social()
		
	}// end class _main
	
	
/* End Of File <_main.inc> */